package com.tieshan.smartwindow.other

import android.util.Log
import timber.log.Timber
import java.io.DataOutputStream
import java.io.IOException
import java.lang.Exception


/*
*   @author Atlantis
*   @data 2021/12/8 15:50
*   简介  
*/
object ALPermissionManager {


    //看门狗程序
    fun reBestartWlan() {
        var process: Process? = null
        var os: DataOutputStream? = null
        try {
            process = Runtime.getRuntime().exec("su")
            os = DataOutputStream(process.outputStream)
            os.writeBytes("ifconfig eth0 down; sleep 2; ifconfig eth0 up \n")
            os.flush()
            os.close()
        } catch (e: Exception) {
            Timber.e("异常"+e.message)
        } finally {
            try {
                os?.close()
                process!!.waitFor()
            } catch (e: Exception) {
                Timber.e("异常2"+e.message)
            }
        }
    }

    //看门狗程序
    fun setIp() {
        var process: Process? = null
        var os: DataOutputStream? = null
        try {
            process = Runtime.getRuntime().exec("su")
            os = DataOutputStream(process.outputStream)
            os.writeBytes("ifconfig eth0 10.0.0.37\n")
            os.flush()
            os.close()
        } catch (e: Exception) {
            Timber.e("异常"+e.message)
        } finally {
            try {
                os?.close()
                process!!.waitFor()
            } catch (e: Exception) {
                Timber.e("异常2"+e.message)
            }
        }
    }

    //看门狗程序
    fun reBoot() {
        var process: Process? = null
        var os: DataOutputStream? = null
        try {
            process = Runtime.getRuntime().exec("su")
            os = DataOutputStream(process.outputStream)
            os.writeBytes("reboot \n")
            os.flush()
            os.close()
        } catch (e: Exception) {
            Timber.e("异常"+e.message)
        } finally {
            try {
                os?.close()
                process!!.waitFor()
            } catch (e: Exception) {
                Timber.e("异常2"+e.message)
            }
        }
    }

    //看门狗程序
    fun watchDog() {
        var process: Process? = null
        var os: DataOutputStream? = null
        try {
            process = Runtime.getRuntime().exec("su")
            os = DataOutputStream(process.outputStream)
            os.writeBytes("echo 180 > /sys/class/lx_mcu/watchdog\n")
            os.flush()
            os.close()
        } catch (e: Exception) {
            Timber.e("异常"+e.message)
        } finally {
            try {
                os?.close()
                process!!.waitFor()
            } catch (e: Exception) {
                Timber.e("异常2"+e.message)
            }
        }
    }

    //apk安装
    fun installAndStart(pakgName: String?, apkPath: String) {
//        String cmd1= "am start -n "+pakgName+"/com.cp.smartwindow.ui.MainActivity\n";
        val cmd1 = "am start -n $pakgName/com.tieshan.smartwindow.ui.MainActivity\n"
        val cmd2 = "pm install -r $apkPath && "
        val cmd = cmd2 + cmd1
        Log.d("mTAG", "installAndStart: $cmd")
        //Runtime对象
        val runtime = Runtime.getRuntime()
        try {
            val localProcess = runtime.exec("su")
            val localOutputStream = localProcess.outputStream
            val localDataOutputStream = DataOutputStream(localOutputStream)
            localDataOutputStream.writeBytes(cmd)
            localDataOutputStream.flush()
        } catch (e: IOException) {
            Log.d("mTAG", "installAndStart: ${e.toString()}")
            e.printStackTrace()
        }
    }

    //打开或者关闭4G
    fun closeOrOpen4G(isOpen:Boolean) {
        val cmd = "svc data ${if (isOpen) "enable" else "disable"} \n"
        val runtime = Runtime.getRuntime()
        try {
            val localProcess = runtime.exec("su")
            val localOutputStream = localProcess.outputStream
            val localDataOutputStream = DataOutputStream(localOutputStream)
            localDataOutputStream.writeBytes(cmd)
            localDataOutputStream.flush()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    //设置系统时间
    fun setTime(date:String) {
        Timber.e("data=$date")
        val cmd = "date $date \n"
        val runtime = Runtime.getRuntime()
        try {
            val localProcess = runtime.exec("su")
            val localOutputStream = localProcess.outputStream
            val localDataOutputStream = DataOutputStream(localOutputStream)
            localDataOutputStream.writeBytes(cmd)
            localDataOutputStream.flush()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    /*
     * 执行命令
     * @param command
     * 1、获取root权限 "chmod 777 "+getPackageCodePath()
     * 2、关机 reboot -p
     * 3、重启 reboot
     */
    fun restart(command: String): Boolean {
        var process: Process? = null
        var os: DataOutputStream? = null
        try {
            process = Runtime.getRuntime().exec("su")
            os = DataOutputStream(process.outputStream)
            os.writeBytes(
                """
                $command
                
                """.trimIndent()
            )
            os.writeBytes("exit\n")
            os.flush()
            process.waitFor()
        } catch (e: Exception) {
            return false
        } finally {
            try {
                os?.close()
                process?.destroy()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return true
    }
}